package com.wgu.modules.service.impl;

import com.wgu.entity.AuthorInfo;
import com.wgu.entity.BookInfo;
import com.wgu.entity.Type;
import com.wgu.modules.dto.AddBookDTO;
import com.wgu.modules.mapper.AddBookMapper;
import com.wgu.modules.repository.BookRepository;
import com.wgu.modules.service.AuthorService;
import com.wgu.modules.service.BookService;
import com.wgu.modules.service.TypeService;
import com.wgu.modules.service.utils.BookUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: w
 * @Date: 2019/7/3 17:10
 * @Version 1.0
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BookServiceImpl implements BookService {
    @Resource
    private BookRepository bookRepository;
    @Resource
    private AddBookMapper addBookMapper;
    @Resource
    private AuthorService authorService;
    @Resource
    private TypeService typeService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBook(AddBookDTO bookDTO) {
        bookRepository.save(addBookMapper.toEntity(bookDTO));
    }

    @Override
    public BookInfo getBookByName(String name) {
        return bookRepository.findByBookName(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void export() {
        List<Map<String,Object>> mapList = bookRepository.findAllQDBook();
        mapList.forEach(map->{
            String bookName = (String)map.get("name");
            String minImgUrl = (String)map.get("minImg");
            String score = (String)map.get("score");
            String author = (String)map.get("author");
            String maxType = (String)map.get("maxType");
            String maxImg = (String)map.get("maxImg");
            String zishu = (String)map.get("zishu");
            String descb = (String)map.get("descb");
            String minType = (String)map.get("minType");
            String biao = (String)map.get("biao");

            // 编辑作者
            AuthorInfo authorInfo = authorService.getAuthor(author);

            // 编辑基础信息
            BookInfo bookInfo = bookRepository.findByBookNameAndAuthorId(bookName,authorInfo.getId()).orElse(new BookInfo());
            bookInfo.setBookName(bookName);
            bookInfo.setAuthorId(authorInfo.getId());
            bookInfo.setMinImgUrl(minImgUrl);
            bookInfo.setScore(score);
            bookInfo.setMaxImgUrl(maxImg);
            bookInfo.setWordNum(zishu);
            bookInfo.setIntroduction(descb);
            bookInfo.setKeyword(BookUtil.wordRemoveSpaces(biao));
            // 默认审核成功
            bookInfo.setBookState("2");
            // 默认连载
            bookInfo.setBookStatus("1");
            // 默认VIP
            bookInfo.setIsVip(true);

            // 编辑类型
            Type max = typeService.getType(maxType);
            Type min = typeService.getType(minType);
            bookInfo.setTypeId1(max.getId());
            bookInfo.setTypeId2(min.getId());

            bookRepository.save(bookInfo);
        });
    }
}
